<!--
 * @Author: Olivia
 * @Date: 2022-11-15 22:09:00
 * @custom_string_Olivia: Less is more
-->
<template>
    <van-button
    v-if="isFollowed"
    class="follow-btn"
    round
    size="small"
    :loading="loading"
    @click="onFollow"
    >已关注</van-button>
    <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="loading"
    @click="onFollow"
    >关注</van-button>
</template>
<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  model: {
    prop: 'isFollowed',
    event: 'update-is_followed'
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    aut_id: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  created () {
  },
  computed: {
  },
  methods: {
    async onFollow () {
      this.loading = true
      try {
        if (this.isFollowed) {
        // 已关注，取消关注
          await deleteFollow(this.aut_id)
        } else {
          // 没有关注，添加关注
          await addFollow(this.aut_id)
        }
        // 更新视图状态
        // this.is_followed = !this.is_followed
        // this.$emit('input', !this.value)
        this.$emit('update-is_followed', !this.isFollowed)
      } catch (error) {
        let msg = '操作失败'
        if (error.response && error.response.status === 400) {
          msg = '自己不能关注自己'
        }
        this.$toast(msg)
      }
      // 关闭按钮的 loading 状态
      this.loading = false
    }
  }
}
</script>
<style lang='less'  scoped>

</style>
